package controllers

import (
	"monitor/models"

	"github.com/astaxie/beego"
)

type KestrelController struct {
	beego.Controller
}

type AddKestrelController struct {
	beego.Controller
}

type DelKestrelController struct {
	beego.Controller
}

func (this *KestrelController) Get() {

	kestrels := models.Rc.LGetAll("kestrel")
	if kestrels != nil {
		statArray := models.KestrelStats(kestrels)
		this.Data["data"] = statArray
	}

	this.TplNames = "kestrel.tpl"
}

func (this *AddKestrelController) Get() {
	app := this.GetString("app")
	this.Data["app"] = app
	this.TplNames = "add_kestrel.tpl"
}

func (this *AddKestrelController) Post() {
	app := this.GetString("app")
	host := this.GetString("host")
	if app != "" {
		models.Rc.LPut("kestrel."+app, host)
		this.Redirect("/stats?app="+app, 302)
	} else {
		models.Rc.LPut("kestrel", host)
		this.Redirect("/kestrel", 302)
	}
}

func (this *DelKestrelController) Get() {
	app := this.GetString("app")
	host := this.GetString("host")
	if app != "" {
		models.Rc.LRem("kestrel."+app, host)
		this.Redirect("/stats?app="+app, 302)
	} else {
		models.Rc.LRem("kestrel", host)
		this.Redirect("/kestrel", 302)
	}
}
